﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NewFormatProject.Infrastructure.Dto.Responses
{
    public enum ResponseCode
    {
        Fail = -1,
        Success = 0
    }
    public class ResponseApiDto
    {
        public ResponseCode code { get; set; }
        public string message { get; set; }
        public object data { get; set; }
        public int affectedRows { get; set; }
        /// <summary>
        /// 根据受影响行数决定返回结果，>0成功，=0失败。如果没有受影响行数请使用Success和Fail函数
        /// </summary>
        /// <param name="affectedRows"></param>
        /// <param name="successMessage"></param>
        /// <param name="failMessage"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ResponseApiDto Create(int affectedRows = 0, string successMessage = "成功了", string failMessage = "失败了", object data = null)
        {
            return new ResponseApiDto
            {
                code = affectedRows>0?ResponseCode.Success:ResponseCode.Fail,
                message = affectedRows > 0 ? successMessage : failMessage,
                data = data,
                affectedRows = affectedRows
            };
        }
        /// <summary>
        /// 该函数直接表示成功，受影响行数为0
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ResponseApiDto Success(string message = "成功了", object data = null)
        {
            return new ResponseApiDto
            {
                code = ResponseCode.Success,
                message = message,
                data = data,
                affectedRows = 0
            };
        }
        /// <summary>
        /// 该函数直接表示失败,受影响行数为0
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ResponseApiDto Fail(string message = "失败了", object data = null)
        {
            return new ResponseApiDto
            {
                code = ResponseCode.Fail,
                message = message,
                data = data,
                affectedRows=0
            };
        }
    }
}
